#include "../inc/Ui/WeatherInfoForm.h"
#include "../inc/Utility/MyParserXml.h"

using namespace Osp::Base;
using namespace Osp::Ui;
using namespace Osp::Ui::Controls;
using namespace Osp::Base::Collection;
using namespace Osp::Graphics;
using namespace Osp::Base;
using namespace Osp::Media;

int WeatherInfoForm::FormId = -1;

WeatherInfoForm::WeatherInfoForm(void) {
}

WeatherInfoForm::~WeatherInfoForm(void) {
}

bool WeatherInfoForm::Initialize() {
	Form::Construct(L"WeatherInfoForm");

	return true;
}

result WeatherInfoForm::OnInitializing(void) {
	result r = E_SUCCESS;
	this->SetSoftkeyActionId(SOFTKEY_1, ID_SOFTKEY_BACK);
	this->AddSoftkeyActionListener(SOFTKEY_1, *this);
	// TODO: Add your initialization code here

	pExpandablelist = static_cast<ExpandableList *> (GetControl(
			"IDC_EXPANDABLELIST1"));
	if (pExpandablelist) {
		pExpandablelist->AddExpandableItemEventListener(*this);
	}
	return r;
}

result WeatherInfoForm::OnTerminating(void) {
	result r = E_SUCCESS;
	this->SetSoftkeyActionId(SOFTKEY_1, ID_SOFTKEY_BACK);

	this->AddSoftkeyActionListener(SOFTKEY_1, *this);
	// TODO: Add your termination code here

	return r;
}

void WeatherInfoForm::OnActionPerformed(const Control& source, int actionId) {
	FormManager *pFormManager = null;
	FormManager::GetInstance(pFormManager);

	switch (actionId) {
	case ID_SOFTKEY_BACK:
		pFormManager->SetCurrentForm(WeatherForm::GetFormId());
		break;
	default:
		break;
	}
}

void WeatherInfoForm::OnItemStateChanged(const Osp::Ui::Control &source,
		int mainIndex, int subIndex, int itemId, int elementId,
		Osp::Ui::ItemStatus status) {
	// TODO: Add your implementation codes here

}

void WeatherInfoForm::OnItemStateChanged(const Osp::Ui::Control &source,
		int mainIndex, int subIndex, int itemId, Osp::Ui::ItemStatus status) {
	// TODO: Add your implementation codes here

}
void WeatherInfoForm::LoadDuLieuVaoExpandableList(
		ExpandableList*pExpandableList) {
	//--------------------------------------------------------------------
	// Create Bitmap
	Image *pImage = new Image();
	pImage->Construct();
	/* Create an item format of the main item*/

	__pMainItemFormat = new CustomListItemFormat();
	__pMainItemFormat->Construct();

	__pMainItemFormat->AddElement(ID_LIST_TEXT, Osp::Graphics::Rectangle(110,
			30, 250, 50), 38);
	__pMainItemFormat->AddElement(ID_LIST_BITMAP, Osp::Graphics::Rectangle(10,
			15, 70, 70));
	/* Create an item format of the sub-item*/
	__pSubItemFormat = new CustomListItemFormat();
	__pSubItemFormat->Construct();
	__pSubItemFormat->AddElement(ID_LIST_TEXT, Osp::Graphics::Rectangle(20, 25,
			450, 50), 32);

	/*Bat dau dua du lieu thoi thiet vao expandlist*/
	MyParserXml *myPa = new MyParserXml();
	LinkedListT<MyCurrentWeather*>*list = new LinkedListT<MyCurrentWeather*> ();
	list = myPa->ThoiTietDuBaoTrongTuan("/Home/ThongTinThoiTiet.xml");
	int id = 0;
	for (int i = 0; i < list->GetCount(); i++) {
		MyCurrentWeather*p = new MyCurrentWeather();
		list->GetAt(i, p);
		/* Add main item*/
		Bitmap *pBitmapNormal = GetBitmapN(p->__myIconCur);
		AddListMainItem(*pExpandableList, p->__myAddress, pBitmapNormal,
				pBitmapNormal);
		/* Add sub-item*/
		String temperature = "Temperature: " + p->__myTemp_fCur + " F->"
				+ p->__myTemp_cCur + " F";
		AddListSubItem(i, *pExpandableList, id++, temperature);
		AddListSubItem(i, *pExpandableList, id++, "Condition: "
				+ p->__myConditionCur);
		delete pBitmapNormal;
	}
	//Ket thuc viec dua du lieu thoi tiet vao expandlist
	/* Display the Form*/
	Draw();
	Show();
}
result WeatherInfoForm::AddListMainItem(ExpandableList& expandableList,
		String itemText, Bitmap* pBitmapNormal, Bitmap* pBitmapFocused) {
	result r = E_SUCCESS;
	// Create a main item of the ExpandableList
	CustomListItem* pMainItem = new CustomListItem();
	pMainItem->Construct(100);
	pMainItem->SetItemFormat(*__pMainItemFormat);
	pMainItem->SetElement(ID_LIST_TEXT, itemText);
	pMainItem->SetElement(ID_LIST_BITMAP, *pBitmapNormal, pBitmapFocused);
	// Add a item to the ExpandableList
	expandableList.AddItem(*pMainItem, ID_LIST_MAINITEM);
	return r;
}

result WeatherInfoForm::AddListSubItem(int indexMainItem,
		ExpandableList& expandableList, int itemId, String itemText) {
	result r = E_SUCCESS;
	// Create a sub-item of the ExpandableList
	CustomListItem* pSubItem = new CustomListItem();
	pSubItem->Construct(100);
	pSubItem->SetItemFormat(*__pSubItemFormat);
	pSubItem->SetElement(ID_LIST_TEXT, itemText);
	// Add a item to the ExpandableList
	expandableList.AddSubItem(indexMainItem, *pSubItem, ID_LIST_SUBITEM);
	return r;
}

Bitmap*
WeatherInfoForm::GetBitmapN(const Osp::Base::String& name) {
	String fullname(L"/Res/fig/");
	fullname.Append(name);
	AppLog("Get bitmap:%S", fullname.GetPointer());
	result isTonTai=File::IsFileExist(fullname);
	if(isTonTai==true)
	{
		Bitmap* pBitmap = null;
		Image* pImage = new Image();

		pImage->Construct();

		if(fullname.EndsWith(L"jpg"))
		{
			pBitmap = pImage->DecodeN(fullname, BITMAP_PIXEL_FORMAT_RGB565);
		}
		else if(fullname.EndsWith(L"bmp"))
		{
			pBitmap = pImage->DecodeN(fullname, BITMAP_PIXEL_FORMAT_RGB565);
		}
		else if(fullname.EndsWith(L"png"))
		{
			pBitmap = pImage->DecodeN(fullname, BITMAP_PIXEL_FORMAT_ARGB8888);
		}
		else if (fullname.EndsWith(L"gif"))
		{
			pBitmap = pImage->DecodeN(fullname, BITMAP_PIXEL_FORMAT_RGB565);
		}
		delete pImage;

		return pBitmap;
	}
	return null;
}
void WeatherInfoForm::DisplayCurrentWeather() {
	MyParserXml *myParserXml = new MyParserXml();
	MyCurrentWeather *pMyCurrentWeather = new MyCurrentWeather();
	pMyCurrentWeather = myParserXml->ThoiTietTrongNgay(
			"/Home/ThongTinThoiTiet.xml");

	Bitmap *pBitmapNormal = GetBitmapN(pMyCurrentWeather->__myIconCur);
	// Create a canvas instance.
	Frame* pFrame = Application::GetInstance()->GetAppFrame()->GetFrame();
	pCanvas = pFrame->GetCanvasN();
	pCanvas->Clear();
	/*Icon*/
	pCanvas->DrawBitmap(Rectangle(35, 160, 100, 100), *pBitmapNormal);
	/*Title of postal*/
	Font pFont;
	pFont.Construct(FONT_STYLE_PLAIN, 37);
	pCanvas->SetFont(pFont);
	AppLog("%S", pMyCurrentWeather->__myAddress.GetPointer());
	pCanvas->DrawText(Point(30, 120), pMyCurrentWeather->__myAddress,
			Color::COLOR_BLUE);
	pFont.Construct(FONT_STYLE_PLAIN, 32);
	pCanvas->SetFont(pFont);
	pCanvas->DrawText(Point(180, 170), pMyCurrentWeather->__myTemp_cCur,
			Color::COLOR_BLUE);
	pCanvas->DrawText(Point(180, 200), pMyCurrentWeather->__myHumidityCur,
			Color::COLOR_BLUE);
	pCanvas->DrawText(Point(180, 240), pMyCurrentWeather->__myWindCur,
			Color::COLOR_BLUE);
	pCanvas->Show();
	AppLog("Show canvas");
	delete pCanvas;
}

void WeatherInfoForm::DisplayCurrentWeatherAndForecast() {
	pExpandablelist->RemoveAllItems();
	LoadDuLieuVaoExpandableList(pExpandablelist);
	DisplayCurrentWeather();
}
